//
//  SignHeaderView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/6.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "SignHeaderView.h"


@interface SignHeaderView(){
    CGPoint _centerPoint;
}

@property(nonatomic,strong)UIButton * backButton;

@property(nonatomic,strong)UILabel * titleLabel;

@property(nonatomic,strong)UILabel * greenbeanLabel;

@property(nonatomic,strong)UILabel * remindLabel;

@property(nonatomic,strong)UIButton * signButton;

@property(nonatomic,strong)UILabel * remindDayLabel;

@property(nonatomic,assign)NSInteger remindDay;


@property(nonatomic,strong)UIBezierPath * path;

@property(nonatomic,strong)SignHeaderCircleView * circleView;

@end
@implementation SignHeaderView

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        
        UIImageView * backgroundImageView = [[UIImageView alloc] initWithFrame:self.bounds];
       
        backgroundImageView.image = [UIImage imageNamed:@"Has-signed-in-background"];
        
        [self addSubview:backgroundImageView];
        
        self.backButton = [[UIButton alloc] init];
        [self.backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.backButton setImage:[UIImage imageNamed:@"nav_return"] forState:UIControlStateNormal];
        self.backButton.size = CGSizeMake(20, 20);
        [self addSubview:self.backButton];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.width,44)];
        self.titleLabel.text = @"签到";
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.font = [UIFont systemFontOfSize:18];
        [self addSubview:self.titleLabel];
        
        self.signButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, Sign_width(71), Sign_height(30))];
        [self.signButton setBackgroundImage:[UIImage imageNamed:@"Already-signed-in"] forState:UIControlStateNormal];
        [self.signButton setTitle:@"签到" forState:UIControlStateNormal];
        [self.signButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.signButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.signButton addTarget:self action:@selector(signButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.signButton];
        self.signButton.layer.shadowOffset = CGSizeMake(2, 2);
        self.signButton.layer.shadowOpacity = 1;
        self.signButton.layer.shadowRadius = 5;
        
        self.greenbeanLabel = [self creatLabel:[UIColor whiteColor] font:[UIFont systemFontOfSize:15]];
        [self addSubview:self.greenbeanLabel];
        
        self.remindLabel = [self creatLabel:HexColor(@"#DCEBDB") font:[UIFont systemFontOfSize:12]];
        [self addSubview:self.remindLabel];
    

        
        self.remindDayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Sign_width(50), Sign_width(50))];
        self.remindDayLabel.backgroundColor = [UIColor whiteColor];
        self.remindDayLabel.textAlignment = NSTextAlignmentCenter;
        [self.remindDayLabel addRadius:self.remindDayLabel.width*0.5];
        self.remindDayLabel.layer.borderWidth = 2;
        self.remindDayLabel.layer.borderColor = HexColor(@"#FCAE05").CGColor;
        self.remindDayLabel.font = [UIFont systemFontOfSize:22];
        [self addSubview:self.remindDayLabel];
        
        
        self.circleView = [[SignHeaderCircleView alloc] initWithFrame:CGRectMake(0, 0,Sign_width(100) , Sign_width(100))];
        [self addSubview:self.circleView];
    }
    return self;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    self.backButton.x = globalMargin;
    self.backButton.centerY = 20+44*0.5;
    
    self.signButton.x = self.width - self.signButton.width - Sign_width(35);
    self.signButton.y = self.height - self.signButton.height - Sign_height(20);
    
    self.greenbeanLabel.size = [self.greenbeanLabel.text sizeInAre:MaxSize font:self.greenbeanLabel.font];
    self.greenbeanLabel.centerX = self.width*0.5;
    self.greenbeanLabel.y = self.titleLabel.botton + globalMargin;
    
    self.remindLabel.size = [self.remindLabel.text sizeInAre:MaxSize font:self.remindLabel.font];
    self.remindLabel.centerX = self.width*0.5;
    self.remindLabel.y = self.greenbeanLabel.botton + globalMargin;
    
    self.remindDayLabel.centerX = self.width*0.5;
    self.remindDayLabel.y = self.height - self.remindDayLabel.height-Sign_height(37);
    
    _centerPoint = self.remindDayLabel.center;
    self.circleView.center = _centerPoint;
    
}

-(void)backButtonClick:(UIButton*)button{
    if(self.delegate&&[self.delegate respondsToSelector:@selector(SignHeaderView:didClickBackButton:)]){
        [self.delegate SignHeaderView:self didClickBackButton:button];
    }
}

-(void)setGreenBean:(NSInteger)count remainedDay:(NSInteger)day{
    self.greenbeanLabel.text = [NSString stringWithFormat:@"我的绿豆:%ld个",count];
    self.remindLabel.text = [NSString stringWithFormat:@"继续签到%ld天将额外奖励300绿豆",day];
    NSString * str = [NSString stringWithFormat:@"%ld/7",7-day];
    NSMutableAttributedString * mstr = [[NSMutableAttributedString alloc] initWithString:str];
    [mstr addAttributes:@{NSForegroundColorAttributeName:HexColor(@"#FCAE05")} range:NSMakeRange(0, 1)];
    [mstr addAttributes:@{NSForegroundColorAttributeName:HexColor(@"#999999")} range:NSMakeRange(1, 2)];
    self.remindDayLabel.attributedText = mstr;
    self.remindDay = day;
    [self setNeedsLayout];
}

-(UILabel*)creatLabel:(UIColor*)textColor font:(UIFont*)font{
    UILabel * label = [[UILabel alloc] init];
    label.textColor = textColor;
    label.font = font;
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

-(void)signButtonClick:(UIButton*)button{
    if (!LoginState) {
        [[self getCurrentVC] presentViewController:[[LXLoginViewController alloc] init] animated:YES completion:nil];
    }else{
        //签到
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [[LXDataManager manager] userSignWithPrama:@{@"user_id":User_id} complete:^(BOOL success, id res) {
            if (success) {
                if(success){
                    //获取签到天数
                    [[LXDataManager manager] getSignDaysWithPrama:@{@"user_id":User_id} complete:^(BOOL success, id res) {
                        if(success){
                            NSInteger signtimes = [res[@"data"][@"signtimes"] integerValue];
                            //获取绿豆数
                            [[LXDataManager manager] getGreenBeansCountWithPrama:@{@"user_id":User_id} complete:^(BOOL success, id res) {
                                if(success){
                                  NSInteger beannums = [res[@"data"][@"beannums"] integerValue];
                                    [LXUser shareUser].bean_nums = res[@"data"][@"beannums"];
                                    [self setGreenBean:beannums remainedDay:7-signtimes];
                                    [SVProgressHUD showSuccessWithStatus:@"签到成功"];
                                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                        [SVProgressHUD dismiss];
                                    });

                                }else{
                                    [SVProgressHUD showErrorWithStatus:@"您今天已经签到了"];
                                    
                                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                        [SVProgressHUD dismiss];
                                    });
                                }
                            }];
                        }else{
                            [SVProgressHUD showErrorWithStatus:@"您今天已经签到了"];
                            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                [SVProgressHUD dismiss];
                            });
                        }
                    }];
                }
            }else{
                [SVProgressHUD showErrorWithStatus:@"您今天已经签到了"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
            }
        }];
    }
    
}


@end


@implementation SignHeaderCircleView

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)setSignDays:(NSInteger)signDays{
    _signDays = signDays;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
//    [RGBAColor(109,198,100,1) set];
    [RGBAColor(51,123,22,0.8) set];
    UIBezierPath * path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width*0.5, self.height*0.5) radius:Sign_width(58)*0.5 startAngle:0 endAngle:M_PI*2 clockwise:YES];
    path1.lineWidth = 8;
    [path1 stroke];
    
    [RGBAColor(109,198,100,1) set];
    UIBezierPath * path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width*0.5, self.height*0.5) radius:Sign_width(74)*0.5 startAngle:0 endAngle:M_PI*2 clockwise:YES];
    path2.lineWidth = 8;
    [path2 stroke];
    
    CGFloat startAngle = M_PI*1.5;
    CGFloat rad = (self.signDays/7.0)*(M_PI*2);
    CGFloat endAngle = startAngle+rad;
    [RGBAColor(237,191,0,1) set];
    UIBezierPath * path3 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width*0.5, self.height*0.5) radius:Sign_width(74)*0.5 startAngle:startAngle endAngle:endAngle clockwise:YES];
    path3.lineWidth = 8;
    [path3 stroke];

}

@end
